package com.autumn.aitraffic.controller;



import com.autumn.aitraffic.api.Result;
import com.autumn.aitraffic.domain.po.Driver;
import com.autumn.aitraffic.domain.po.User;
import com.autumn.aitraffic.domain.vo.UserVo;
import com.autumn.aitraffic.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wzx
 * @since 2025-01-17
 */
@RestController
@RequestMapping("/personal")
@CrossOrigin(origins = "*")
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("admin/list")
    public Result getUserList(@RequestParam int pageNum, @RequestParam int pageSize,
                              @RequestParam(required = false) String name,
                              @RequestParam(required= false) String phoneNumber
            ,@RequestParam(required = false) Integer level
    ) {
        Page page = new Page(pageNum, pageSize);
        User user = new User();
        if (name != null) {
            user.setUsername(name);
        }
        if (phoneNumber != null) {
            user.setTelephone(phoneNumber);
        }
        if (level!=null){
            user.setLevel(level.toString());
        }
        return   userService.getUserList(page,user);
    }
    @PostMapping("admin/add")
    public Result addUser(@RequestBody UserVo user) {
        return  userService.addUser(user);
    }
    @PutMapping("admin/update")
    public Result updateUser(@RequestBody UserVo user) {
        return userService.updateUser(user);
    }
    @DeleteMapping("admin/delete/{id}")
    public Result deleteUser(@PathVariable Long id) {
        return userService.deleteUserById(id);
    }
    @DeleteMapping("admin/deleteBatch")
    public Result deleteUserBatch(@RequestBody List<Long> ids) {
        for (Long id : ids) {
            userService.deleteUserById(id);
        }
        return Result.succ(null,null);
    }
}
